package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.BusinessEnum;
import com.powernode.domain.MemberCollection;
import com.powernode.domain.Prod;
import com.powernode.ex.handler.BusinessException;
import com.powernode.feign.MemberProductFeign;
import com.powernode.mapper.MemberCollectionMapper;
import com.powernode.model.Result;
import com.powernode.service.MemberCollectionService;
import com.powernode.util.AuthUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 贺
* @description 针对表【member_collection】的数据库操作Service实现
* @createDate 2024-07-19 19:17:03
*/
@Service
public class MemberCollectionServiceImpl extends ServiceImpl<MemberCollectionMapper, MemberCollection>
    implements MemberCollectionService{

    @Resource
    private MemberCollectionMapper memberCollectionMapper;
    @Resource
    private MemberProductFeign memberProductFeign;

    /**
     * 查询会员收藏商品的数量
     *
     * @return
     */
    @Override
    public Long queryMemberCollectionCount() {
        // 获取会员的openId
        String openId = AuthUtils.getMemberOpenId();
        // 查询会员收藏商品的数量
        return memberCollectionMapper.selectCount(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, openId));
    }

    /**
     * 分页查询会员收藏的商品列表
     *
     * @param current
     * @param size
     * @return
     */
    @Override
    public Page<Prod> queryMemberCollectionProdPage(Long current, Long size) {

        // 创建商品分页对象
        Page<Prod> prodPage = new Page<>(current, size);

        // 获取会员的openId
        String openId = AuthUtils.getMemberOpenId();

        // 根据会员openId分页查询会员和商品收藏关系记录
        Page<MemberCollection> memberCollectionPage = new Page<>(current, size);
        memberCollectionPage = memberCollectionMapper.selectPage(memberCollectionPage, new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, openId)
                .orderByDesc(MemberCollection::getCreateTime));
        // 获取会员和商品收藏关系记录集合
        List<MemberCollection> memberCollectionList = memberCollectionPage.getRecords();
        // 判断会员和商品收藏关系记录集合是否为空
        if (CollectionUtil.isEmpty(memberCollectionList) || memberCollectionList.size() == 0) {
            return prodPage;
        }
        // 会员和商品收藏关系记录集合不为空，从会员和商品收藏关系记录集合中的商品id集合
        List<Long> prodIdList = memberCollectionList.stream().map(MemberCollection::getProdId).collect(Collectors.toList());
        // 远程调用product-service：根据商品id集合查询商品列表
        Result<List<Prod>> result = memberProductFeign.getProdListByIds(prodIdList);
        // 判断远程调用是否成功
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(result.getCode())) {
            throw new BusinessException("远程调用product-service接口：根据商品id集合查询商品对象集合失败");
        }
        // 获取商品集合
        List<Prod> prodList = result.getData();

        // 封装数据
        prodPage.setRecords(prodList);
        prodPage.setTotal(memberCollectionPage.getTotal());
        prodPage.setPages(memberCollectionPage.getPages());

        return prodPage;
    }

    /**
     * 添加或取消收藏商品
     *
     * @param openId
     * @param prodId
     * @return
     */
    @Override
    public Boolean addOrCancelMemberCollection(String openId, Long prodId) {
        // 根据openId和商品id查询会员和商品收藏关系记录
        MemberCollection memberCollection = memberCollectionMapper.selectOne(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, openId)
                .eq(MemberCollection::getProdId, prodId));
        // 判断会员和商品收藏关系记录是否存在
        if (ObjectUtil.isNull(memberCollection)) {
            // 会员和商品收藏关系记录不存在，当前商品未被收藏，添加收藏
            memberCollection = new MemberCollection();
            memberCollection.setOpenId(openId);
            memberCollection.setProdId(prodId);
            memberCollection.setCreateTime(new Date());
            return memberCollectionMapper.insert(memberCollection) > 0;
        }
        // 会员和商品收藏关系记录存在，当前商品已被收藏，取消收藏
        return memberCollectionMapper.deleteById(memberCollection.getId()) > 0;
    }
}




